package com.ccdc.lbcmsystem.controller;

import com.ccdc.lbcmsystem.mapper.RoleMapper;
import com.ccdc.lbcmsystem.mapper.UserMapper;
import com.ccdc.lbcmsystem.pojo.Role;
import com.ccdc.lbcmsystem.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Controller
public class CommunityController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;
    @RequestMapping("/communityList")
    public String communityList(Model model,@RequestParam(defaultValue = "1") Integer startPage,
                                @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<User> communityUsers = userMapper.queryCommunityList();
        PageInfo<User> communityUsersPageInfo = new PageInfo<>(communityUsers);
        model.addAttribute("communityUsersPageInfo", communityUsersPageInfo);
        model.addAttribute("communityUsers",communityUsers);
        return "community/communityList";
    }

    @RequestMapping("/searchCommunity")
    public String searchCommunity(String userName, Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                  @RequestParam(defaultValue = "10") Integer pageSize, HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<User> communityUsers = userMapper.queryListCommunityListByUserName(userName);
        PageInfo<User> communityUsersPageInfo = new PageInfo<>(communityUsers);
        session.setAttribute("userName",userName);
        model.addAttribute("communityUsersPageInfo", communityUsersPageInfo);
        model.addAttribute("communityUsers",communityUsers);
        return "community/communitySearchList";
    }

    @RequestMapping("/communityDelete")
    @ResponseBody
    public String communityDelete(Integer id, Model model){
        String msg = "";
        try {
            userMapper.delUserById(id);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "删除失败";
            return msg;
        }
    }

    @RequestMapping("/toCommunityAdd")
    public String toCommunityAdd(){
        return "community/communityAdd";
    }
    @RequestMapping("/toCommunityEdit/{id}")
    public String toCommunityEdit(@PathVariable("id") Integer id, Model model){
        User user = userMapper.queryUserById(id);
        List<Role> roles = roleMapper.queryAll();
        model.addAttribute("roles",roles);
        model.addAttribute("user",user);
        return "community/communityEdit";
    }
    @RequestMapping("/communityEdit")
    @ResponseBody
    public String communityEdit(Integer id, String name, String passWord,
                            String email, String tel, String role){
        String msg = "";
        if (StringUtils.isBlank(name) || name.equals("")){
            msg = "请输入姓名";
            return msg;
        }
        if (StringUtils.isBlank(passWord) || passWord.equals("")){
            msg = "请输入密码";
            return msg;
        }
        if (StringUtils.isBlank(email) || email.equals("")){
            msg = "请输入邮箱";
            return msg;
        }
        if (StringUtils.isBlank(tel) || tel.equals("")){
            msg = "请输入电话号码";
            return msg;
        }
        String regEx3 = "^[\u4e00-\u9fa5]{0,6}$";
        if (!name.matches(regEx3)){
            msg = "姓名只能由汉字组成";
            return msg;
        }
        String regEx2 = "^[A-Za-z0-9]+$";
        if (!passWord.matches(regEx2)){
            msg = "密码只能由数字和字母组成";
            return msg;
        }
        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p = Pattern.compile(regEx1);
        Matcher m = p.matcher(email);
        if (!m.matches() && !email.equals("")){
            msg = "邮箱格式错误,请重新输入";
            return msg;
        }
        String REG_EXP = "^([0-9]{3}-?[0-9]{8}|[0-9]{4}-?[0-9]{7})$";
        if (!tel.matches(REG_EXP) && !tel.equals("")){
            msg = "手机号码错误,请重新输入";
            return msg;
        }
        else {
            userMapper.updateUserById(id,name,passWord,email,tel,role);
            msg = "OK";
            return msg;
        }

    }


}
